//
//  Seach_FiltrateBrand_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/20.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Seach_FiltrateBrand_Tbv_WX.h"
#import "MainSeachBrandCell.h"
#import "MainSeachBCell.h"
#import "UITableView+SCIndexView.h"
#import "Seach_FiltrateBrand_CellWX.h"


@implementation Seach_FiltrateBrand_Tbv_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self def];
        [self setUI];
        [self setMasonry];
        [self Starefreshing];
    }
    return self;
}

-(void)def{
    
    [self registerNib:[UINib nibWithNibName:@"MainSeachBrandCell" bundle:nil] forCellReuseIdentifier:@"MainSeachBrandCell"];
    [self registerNib:[UINib nibWithNibName:@"MainSeachBCell" bundle:nil] forCellReuseIdentifier:@"MainSeachBCell"];
    
    self.delegate = self;
    self.dataSource = self;
    
    SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle];
    [self setConfiguration:configuration];
    self.sc_indexViewConfiguration = configuration;
    self.sc_translucentForTableViewInNavigationBar = self.translucent;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rowHeight = UITableViewAutomaticDimension;
    self.backgroundColor = [UIColor whiteColor];
    

    self.sc_indexViewDataSource = kUSER_Filtrate_MANAGER.arrBrandBigWord;

}

-(void)setUI{
    
}

-(void)setMasonry{
    
}

-(void)Starefreshing{
    
    
    
//    [[serviceDefualt loadGoodsSearchConditions:[searchConditionsModel class]]subscribeNext:^(ResponseData * x) {
//        if (x.success) {
//            self.model = x.responseData;
//
//            NSDictionary *dic =[self.model.brandInfo yy_modelToJSONObject];
//
//            self.dicModel = [[NSMutableDictionary alloc]initWithDictionary:dic];
//
//            NSMutableArray *indexViewDataSource = [NSMutableArray array];
//
//            NSArray *newArray = [[self.dicModel allKeys] sortedArrayUsingSelector:@selector(compare:)];
//            NSMutableArray *arrM = [[NSMutableArray alloc]init];
//            NSMutableArray *arrMBigWord = [[NSMutableArray alloc]init];
//            NSMutableArray *arrMModel = [[NSMutableArray alloc]init];
//
//            for (NSString *str in newArray) {
//                NSArray *arr = self.dicModel[str];
//                if (arr.count>0) {
//
//                    //填充字母
//                    [indexViewDataSource addObject:str];
//                    [arrM addObject: self.dicModel[str]];
//                    [arrMBigWord addObject:str];
//
//                    NSMutableArray *arrMdicModel = [[NSMutableArray alloc]init];
//                    for (NSDictionary *dic in self.dicModel[str]) {
//                        branddModel *model = [branddModel yy_modelWithDictionary:dic];
//                        [arrMdicModel addObject:model];
//                    }
//
//                    [arrMModel addObject:arrMdicModel];
//
//                    NSLog(@"");
//
//
//                }
//            }
//
//            [self setUI];
//
//            if (self.hasSearch) {
//                [indexViewDataSource insertObject:UITableViewIndexSearch atIndex:0];
//            }
//
//            self.sc_indexViewDataSource = indexViewDataSource.copy;
//            NSLog(@"");
//            kUSER_Filtrate_MANAGER.arrBrand = [[NSMutableArray alloc]initWithArray:arrMModel];
//            kUSER_Filtrate_MANAGER.arrBrandBigWord = arrMBigWord;
//            kUSER_Filtrate_MANAGER.arrBrandSelect = [[NSMutableArray  alloc]init];
//            [self reloadData];
//        }else{
//            [Tools showTips:@"获取品牌数据失败"];
//        }
//
//
//    }];
    
}



#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"indexPath == %@",indexPath);
    NSLog(@"");
    if (self.block) {
        self.block(kUSER_Filtrate_MANAGER.arrAttribute[0].child[indexPath.section].data[indexPath.row]);
//        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (kUSER_Filtrate_MANAGER.arrAttribute.count!=0) {
        NSArray *arr = kUSER_Filtrate_MANAGER.arrAttribute[0].child;
        return arr.count;
    }
    return 0;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = kUSER_Filtrate_MANAGER.arrAttribute[0].child[section].data;
    
  
    //0 1 2
    return arr.count/3+(arr.count%3==0?0:1);
    
    if (arr.count!=0) {
        NSInteger index= arr.count/3;
        if (index == 0) {
            return 1;
        }
        if (index%3 ==0) {
            return index;
        }
        return index+1;
    }
    return 0;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getSeach_FiltrateBrand_CellWX:indexPath];
//    return [self getMainSeachBrandCell:indexPath];

}

-(UITableViewCell *)getSeach_FiltrateBrand_CellWX:(NSIndexPath *)indexPath{
        static NSString *sectionCell = @"Seach_FiltrateBrand_CellWX";
        Seach_FiltrateBrand_CellWX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[Seach_FiltrateBrand_CellWX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    
        cell.indexPath = indexPath;
        [cell funcRefloadCell];
    
//        cell.arr =
        return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
//    view.backgroundColor = [UIColor redColor];
    QMUILabel *lab  =[[QMUILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH-40, 15)];
    //    NSArray *arr = [self.dicModel allKeys];
//    lab.backgroundColor = [UIColor greenColor];
    if ([[NSString stringWithFormat:@"%@",kUSER_Filtrate_MANAGER.arrBrandBigWord[section]] isEqualToString:@"热"]) {
        lab.text =@"热门";
    } else {
        lab.text =[NSString stringWithFormat:@"%@",kUSER_Filtrate_MANAGER.arrBrandBigWord[section]];
    }
    lab.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:13];
    lab.centerY = view.centerY;
    lab.textColor = [UIColor colorWithHex:@"333333"];
    [view addSubview:lab];
    return view;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat heightCell = 0;
    /* TODO: ================= cell 高度通过类型区分 =================*/
    
    heightCell = 44;
    
    return heightCell;
}

//设置区头不悬浮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 50;
    
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}

#pragma mark - Getter and Setter


-(void)setConfiguration:(SCIndexViewConfiguration*)configuration{
    
    configuration.indicatorBackgroundColor = baseColorTheme;
    configuration.indicatorHeight = 50;
    configuration.indicatorRightMargin = 50;
    configuration.indicatorCornerRadius = 20;
    configuration.indexItemBackgroundColor = [UIColor clearColor];
    configuration.indexItemSelectedBackgroundColor = [UIColor whiteColor];
    configuration.indexItemHeight = 14;
    configuration.indexItemRightMargin = 2;
    configuration.indexItemsSpace = 2;
    
    configuration.indexItemTextColor = [UIColor colorWithHex:@"333333"];
    configuration.indicatorTextColor = [UIColor whiteColor];//选中文字颜色
    configuration.indexItemSelectedTextColor = baseColorTheme;//右边选中文字颜色
    configuration.indicatorHeight = 60;
    
    //    @property (nonatomic, assign) CGFloat indexItemHeight;                      // 索引元素高度
    
}

-(NSMutableDictionary *)dicModel{
    if (!_dicModel) {
        _dicModel = [[NSMutableDictionary alloc]init];
    }
    return _dicModel;
}





@end
